package com.cqjtu.store.product.service.impl;

import com.cqjtu.store.common.PageVO;
import com.cqjtu.store.common.ServiceCode;
import com.cqjtu.store.common.ServiceException;
import com.cqjtu.store.product.mapper.ProductCategoryMapper;
import com.cqjtu.store.product.pojo.dto.ProductCategoryDTO;
import com.cqjtu.store.product.pojo.dto.ProductCategoryPageDTO;
import com.cqjtu.store.product.pojo.entity.ProductCategory;
import com.cqjtu.store.product.pojo.vo.ProductCategoryVO;
import com.cqjtu.store.product.service.ProductCategoryService;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: cqjtu-store
 * @Titile: ProductCategoryServiceImpl
 * @Author: Lucky
 * @Description: 商品分类的业务类
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Override
    public List<ProductCategoryVO> getList() {
        List<ProductCategory> productCategoryList = productCategoryMapper.getList();
        //创建vo列表对象
        List<ProductCategoryVO> productCategoryVOList = new ArrayList<>();
        productCategoryList.forEach(productCategory -> {
            ProductCategoryVO productCategoryVO = new ProductCategoryVO();
            productCategoryVO.setId(productCategory.getId());
            productCategoryVO.setName(productCategory.getName());
            productCategoryVO.setParentId(productCategory.getParentId());
            productCategoryVO.setIsParent(productCategory.getIsParent());
            productCategoryVO.setSortOrder(productCategory.getSortOrder());
            productCategoryVO.setStatus(productCategory.getStatus());
            productCategoryVO.setCreatedTime(productCategory.getCreatedTime());
            productCategoryVO.setCreatedUser(productCategory.getCreatedUser());
            productCategoryVO.setModifiedTime(productCategory.getModifiedTime());
            productCategoryVO.setModifiedUser(productCategory.getModifiedUser());
            productCategoryVOList.add(productCategoryVO);
        });
        return productCategoryVOList;
    }

    @Override
    public void addCategory(ProductCategoryDTO productCategoryDTO, UserLoginVO userLoginVO) {
        ProductCategory productCategory = new ProductCategory();


            productCategory.setName(productCategoryDTO.getName());
            // 默认正常使用
            productCategory.setParentId(productCategoryDTO.getParentId());
            // 默认为子类
            productCategory.setIsParent(0);
            productCategory.setSortOrder(productCategoryDTO.getSortOrder());
            productCategory.setStatus(productCategoryDTO.getStatus());
            productCategory.setCreatedUser(userLoginVO.getUsername());
            int result = productCategoryMapper.insert(productCategory); // 插入数据
            if (result != 1) {
                throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"插入分类失败");
            }

    }
    @Override
    public void deleteCategory(Integer categoryId, UserLoginVO userLoginVO) {
        ProductCategory productCategory = productCategoryMapper.getById(categoryId);
        System.out.println(productCategory);
        if (productCategory == null) {
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"分类不存在");
        }
        if (productCategory.getIsParent() == 1) {
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"父分类不能删除");
        }
        int result = productCategoryMapper.delete(categoryId);
        if (result != 1) {
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"删除分类失败");
        }
    }

    @Override
    public List<ProductCategoryVO> getCategoryList(Integer parentId) {
        List<ProductCategory> productCategoryList = productCategoryMapper.getListByParentId(parentId);
        if (productCategoryList == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"找不到该分类的子分类");
        }
        List<ProductCategoryVO> productCategoryVOList = new ArrayList<>();
        productCategoryList.forEach(productCategory -> {
            ProductCategoryVO productCategoryVO = new ProductCategoryVO();
            productCategoryVO.setId(productCategory.getId());
            productCategoryVO.setName(productCategory.getName());
            productCategoryVO.setParentId(productCategory.getParentId());
            productCategoryVO.setIsParent(productCategory.getIsParent());
            productCategoryVO.setSortOrder(productCategory.getSortOrder());
            productCategoryVO.setStatus(productCategory.getStatus());
            productCategoryVO.setCreatedTime(productCategory.getCreatedTime());
            productCategoryVO.setCreatedUser(productCategory.getCreatedUser());
            productCategoryVO.setModifiedTime(productCategory.getModifiedTime());
            productCategoryVO.setModifiedUser(productCategory.getModifiedUser());
            productCategoryVOList.add(productCategoryVO);
        });
        return productCategoryVOList;
    }

    @Override
    public ProductCategoryVO getCategoryById(Integer id) {
        ProductCategory productCategory = productCategoryMapper.getById(id);
        if (productCategory == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"找不到该分类");
        }
        ProductCategoryVO productCategoryVO = new ProductCategoryVO();
        productCategoryVO.setId(productCategory.getId());
        productCategoryVO.setName(productCategory.getName());
        productCategoryVO.setParentId(productCategory.getParentId());
        productCategoryVO.setIsParent(productCategory.getIsParent());
        productCategoryVO.setSortOrder(productCategory.getSortOrder());
        productCategoryVO.setStatus(productCategory.getStatus());
        productCategoryVO.setCreatedTime(productCategory.getCreatedTime());
        productCategoryVO.setCreatedUser(productCategory.getCreatedUser());
        productCategoryVO.setModifiedTime(productCategory.getModifiedTime());
        productCategoryVO.setModifiedUser(productCategory.getModifiedUser());
        return productCategoryVO;
    }

    @Override
    public PageInfo<ProductCategory> getCategoryByPage(ProductCategoryPageDTO categoryPageDTO) {

        //开启分页：设置分页参数:页码值；每页显示的数量
        PageHelper.startPage(categoryPageDTO.getPageIndex(),categoryPageDTO.getPageSize());
        if(categoryPageDTO.getIsParent() != null && categoryPageDTO.getIsParent() == 1){
            System.out.println(categoryPageDTO.getIsParent());
            //查询所有子分类
            List<ProductCategory> productCategoryList = productCategoryMapper.getListByIsParent(categoryPageDTO.getIsParent());
            if(productCategoryList == null){
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"该商品分类没有子分类");
            }

            PageInfo<ProductCategory> pageInfo = new PageInfo<>(productCategoryList);
            return pageInfo;
        }else{
            //根据分页返回所有分类
            List<ProductCategory> productCategoryList = productCategoryMapper.getList();
            if(productCategoryList == null){
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"无分类");
            }
            PageInfo<ProductCategory> pageInfo = new PageInfo<>(productCategoryList);
            return pageInfo;
        }
    }
    @Override
    public void updateCategory(ProductCategoryDTO productCategoryDTO, UserLoginVO userLoginVO) {
        ProductCategory productCategory = productCategoryMapper.getById(productCategoryDTO.getId());
        if (productCategory == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"找不到该分类");
        }
        productCategory.setName(productCategoryDTO.getName());
        productCategory.setParentId(productCategoryDTO.getParentId());
        productCategory.setIsParent(productCategoryDTO.getIsParent());
        productCategory.setSortOrder(productCategoryDTO.getSortOrder());
        productCategory.setStatus(productCategoryDTO.getStatus());
        productCategory.setModifiedUser(userLoginVO.getUsername());

        int result = productCategoryMapper.update(productCategory);
        if (result != 1) {
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"修改分类失败");
        }
    }

}
